#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int n;
std::string s;

int calc(std::string & s)
{
	int count_op = 0, count_d = 0, ans = 0;
	for (int i = 0; i < (int)s.size(); i++)
		if (std::isdigit(s[i])) count_d++;
		else					count_op++;
	if (count_op + 1 > count_d){
		int del = count_op + 1 - count_d;
		s = std::string(del, '1') + s;
		ans = del;
	}

	int t = 0, len = s.size(), pos = len-1;

	for (int i = 0; i < len; i++) {
		if (std::isdigit(s[i])) t++;
		else {
			if (t < 2) {
				pos = len - 1;
				for (; !std::isdigit(s[pos]); pos--);
				std::swap(s[i], s[pos]);
				ans++;
				i--;
			} else {
				t--;
			}
		}
	}
	return ans;
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n;
	for (int i = 0; i < n; i++) {
		std::cin >> s;
		std::cout << calc(s) << '\n';
	}
}
